import json

from django.core.urlresolvers import reverse
from django.test import Client, TestCase

from main.models.user import User
from main.models.subscription import Subscription


class UserTestCase(TestCase):
    def setUp(self):
        self.c = Client()
        User.enabled.create_user('15010101010')

    # 测试注册
    def test_register(self):
        username = '13010101010'
        r = self.c.post(reverse('user:login'), {'username': username})
        r = json.loads(r.content.decode('utf8'))
        self.assertEqual(r['count'], 0)

    # 测试登录
    def test_login(self):
        username = '15010101010'
        r = self.c.post(reverse('user:login'), {'username': username})
        r = json.loads(r.content.decode('utf8'))
        self.assertEqual(r['count'], 0)


class SubscriptionTestCase(TestCase):
    def setUp(self):
        self.c = Client()
        User.enabled.create_user('000')
        user1 = User.enabled.create_user('111')
        user2 = User.enabled.create_user('222')
        self.s1 = Subscription(owner=user1, name='aaa')
        self.s1.save()
        self.s2 = Subscription(owner=user2, name='bbb')
        self.s2.save()
        self.s3 = Subscription(owner=user2, name='ccc')
        self.s3.save()
        self.s4 = Subscription(owner=user2, name='ddd')
        self.s4.save()
        self.s5 = Subscription(owner=user2, name='eee')
        self.s5.save()
        self.s6 = Subscription(owner=user2, name='fff')
        self.s6.save()
        self.s7 = Subscription(owner=user2, name='ggg')
        self.s7.save()
        self.s8 = Subscription(owner=user2, name='hhh')
        self.s8.save()
        self.s9 = Subscription(owner=user2, name='iii')
        self.s9.save()
        self.s10 = Subscription(owner=user2, name='jjj')
        self.s10.save()
        self.s11 = Subscription(owner=user2, name='kkk')
        self.s11.save()

    # 测试关注后登录
    def test_login(self):
        username = '000'
        r = self.c.post(reverse('user:follow_subscription',
                                kwargs={'subscription_id': self.s1.id}),
                        {'username': username})
        self.assertEqual(r.status_code, 200)

        r = self.c.post(reverse('user:login'), {'username': username})
        r = json.loads(r.content.decode('utf8'))
        self.assertEqual(r['count'], 1)
        self.assertEqual(r['list'][0]['id'], 1)
        self.assertEqual(r['list'][0]['name'], 'aaa')

    # 测试获取公众号列表
    def test_get_all_subscriptions(self):
        username = '000'
        r = self.c.get(reverse('subscription:list'), {'username': username})
        r = json.loads(r.content.decode('utf8'))
        self.assertEqual(r['count'], 11)
        self.assertEquals(r['list'][0]['name'], 'kkk')
        self.assertEquals(r['list'][10]['name'], 'aaa')
        # offset = 5
        # limit = 6
        # r = self.c.get(reverse('subscription:list'), {'username': username,
        #                                               'offset': offset,
        #                                               'limit': limit})
        # r = json.loads(r.content.decode('utf8'))
        # self.assertEqual(r['count'], 11)
        # self.assertEquals(r['list'][0]['name'], 'fff')
        # self.assertEquals(r['list'][5]['name'], 'aaa')

    def test_follow_and_related(self):
        # 成功关注公众号
        username = '000'
        r = self.c.post(reverse('user:follow_subscription',
                                kwargs={'subscription_id': self.s1.id}),
                        {'username': username})
        self.assertEqual(r.status_code, 200)

        # 不能重复关注公众号
        username = '000'
        r = self.c.post(reverse('user:follow_subscription',
                                kwargs={'subscription_id': self.s1.id}),
                        {'username': username})
        self.assertEqual(r.status_code, 404)

        # 不能关注自己的公众号
        username = '111'
        r = self.c.post(reverse('user:follow_subscription',
                                kwargs={'subscription_id': self.s1.id}),
                        {'username': username})
        self.assertEqual(r.status_code, 403)

        # 查看公众号的粉丝
        username = '111'
        r = self.c.get(reverse('subscription:subscription_followers',
                               kwargs={'subscription_id': self.s1.id}),
                        {'username': username})
        r = json.loads(r.content.decode('utf8'))
        self.assertEqual(r['count'], 1)

        # 给粉丝群发消息
        username = '222'
        r = self.c.post(reverse('subscription:subscription_messages',
                                kwargs={'subscription_id': self.s2.id}),
                        {'username': username, 'content': 'test message'})
        self.assertEqual(r.status_code, 403)

        username = '111'
        r = self.c.post(reverse('subscription:subscription_messages',
                                kwargs={'subscription_id': self.s1.id}),
                        {'username': username, 'content': 'test message'})
        self.assertEqual(r.status_code, 200)

        # 公众号获取历史发送的消息
        username = '222'
        r = self.c.get(reverse('subscription:subscription_messages',
                               kwargs={'subscription_id': self.s2.id}),
                        {'username': username})
        r = json.loads(r.content.decode('utf8'))
        self.assertEqual(r['count'], 0)

        username = '111'
        r = self.c.get(reverse('subscription:subscription_messages',
                               kwargs={'subscription_id': self.s1.id}),
                        {'username': username})
        r = json.loads(r.content.decode('utf8'))
        self.assertEqual(r['count'], 1)

        # 用户接收消息
        username = '000'
        r = self.c.get(reverse('user:user_messages'),
                        {'username': username})
        r = json.loads(r.content.decode('utf8'))
        self.assertEqual(r['count'], 1)
